/**
 * Created by xiebang
 * Description
 * User:小谢
 * Date:2021-09-16
 * Time:10:00
 */
class Money implements  Cloneable{
    public double money = 12.5;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
//这是一个空接口的作用：标记接口，如果一个类实现了clon方法，说明他是能被clon的
class Person implements  Cloneable{
    public String name = "xie";
    public Money money = new Money();
    int a = 10;
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
    //objiect类的clon方法 所以的类都默认继承 object类

    @Override
    protected Object clone() throws CloneNotSupportedException {
//        return super.clone();
        Person personClon = (Person) super.clone();
        personClon.money = (Money) this.money.clone();
        return personClon;
    }
}
public class TestDemo2 {
    public static void main(String[] args)throws CloneNotSupportedException {
        Person person = new Person();
        //System.out.println(person);//不重写tostring打印地址

        //克隆
        Person person1 = (Person) person.clone();
        //System.out.println(person1);


        //浅拷贝，只拷贝了地址
        //克隆对象里面的对象
        System.out.println(person.money.money);
        System.out.println(person1.money.money);
        System.out.println("==========");
        person1.money.money = 99;
        System.out.println(person.money.money);
        System.out.println(person1.money.money);

    }
    public static void main2(String[] args)throws CloneNotSupportedException {
        Person person = new Person();
        //System.out.println(person);//不重写tostring打印地址

        //克隆
        Person person1 = (Person) person.clone();
        //System.out.println(person1);

        System.out.println(person.a);
        System.out.println(person1.a);
        System.out.println("==========");
        person1.a = 99;
        System.out.println(person.a);
        System.out.println(person1.a);

    }
    public static void main1(String[] args)throws CloneNotSupportedException {
        Person person = new Person();
        System.out.println(person);//不重写tostring打印地址

        //克隆
        Person person1 = (Person) person.clone();
        System.out.println(person1);

    }
}
